package bean.Resource;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.springframework.core.io.ClassPathResource;

import java.io.IOException;
import java.util.List;

/**
 * 测试ClassPathResource
 */
public class ClassPathResourceTest {
    public static void main(String[] args) {
        ClassPathResource url = new ClassPathResource("beans.xml");
        System.out.println("FileName "+url.getFilename());
        System.out.println("Description: "+url.getDescription());
        System.out.println(url.exists());
//        UrlResourceTest.showResourceFile(url);
        //使用DOM4J
        try {
            SAXReader reader = new SAXReader();
            Document doc = reader.read(url.getURL());;
            Element element = doc.getRootElement();
            traversalXML(element);
        } catch (DocumentException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    public static void traversalXML(Element element){
        List<Element> list = element.elements();
        if (list.size() >0 ){
            for(Element e : list){
                traversalXML(e);
            }
        }else{
            System.out.println(element.getText());
        }
    }
}
